package com.gaohe.dcp.service.impl;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.util.CellRangeAddress;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ExcelCreator {

    public static void main(String[] args) throws IOException {
        String filePath = "D:\\o1.xlsx";
        File file = new File(filePath);

        Workbook workbook;
        Sheet sheet;
        // 判断文件是否存在
        if (file.exists()) {
            // 如果文件存在，打开现有工作簿
            try (FileInputStream fis = new FileInputStream(file)) {
                workbook = new XSSFWorkbook(fis);
            }
            // 检查Sheet1是否存在
            sheet = workbook.getSheet("Sheet1");
            if (sheet != null) {
                System.out.println("Sheet1已存在，跳过模板创建。");
                return; // 如果Sheet1存在，直接退出
            } else {
                // 如果Sheet1不存在，创建Sheet1
                sheet = workbook.createSheet("Sheet1");
            }
        } else {
            // 如果文件不存在，创建新工作簿和Sheet1
            workbook = new XSSFWorkbook();
            sheet = workbook.createSheet("Sheet1");
        }

        // 创建字体样式
        Font font = workbook.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short) 12);

        // 创建单元格样式
        CellStyle style = workbook.createCellStyle();
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);
        style.setVerticalAlignment(VerticalAlignment.CENTER);
        style.setBorderTop(BorderStyle.THIN);
        style.setBorderBottom(BorderStyle.THIN);
        style.setBorderLeft(BorderStyle.THIN);
        style.setBorderRight(BorderStyle.THIN);

        // 创建第一行
        Row row1 = sheet.createRow(0);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("姓名");
        cell1.setCellStyle(style);

        Cell cell2 = row1.createCell(1);
        cell2.setCellValue("门诊号");
        cell2.setCellStyle(style);

        Cell cell3 = row1.createCell(2);
        cell3.setCellValue("住院号");
        cell3.setCellStyle(style);

        Cell cell4 = row1.createCell(3);
        cell4.setCellValue("就诊卡号");
        cell4.setCellStyle(style);

        //第二部分
        Cell cell5 = row1.createCell(4);
        cell5.setCellValue("社交能力");
        cell5.setCellStyle(style);

        Cell cell6 = row1.createCell(10);
        cell6.setCellValue("行为问题");
        cell6.setCellStyle(style);

        // 创建第二行
        Row row2 = sheet.createRow(1);
        Cell cell7 = row2.createCell(4);
        cell7.setCellValue("活动能力");
        cell7.setCellStyle(style);

        Cell cell8 = row2.createCell(6);
        cell8.setCellValue("社交情况");
        cell8.setCellStyle(style);

        Cell cell9 = row2.createCell(8);
        cell9.setCellValue("学校情况");
        cell9.setCellStyle(style);

        //第三部分
        for (int i = 0; i < 9; i++) {
            Cell c1 = row2.createCell(10+3*i);
            String value = "因子"+(i+1);
            c1.setCellValue(value);
            c1.setCellStyle(style);
        }

        //第三行
        Row row3 = sheet.createRow(2);
        for (int i = 0; i < 3; i++) {
            Cell c1 = row3.createCell(4 + 2 * i);
            c1.setCellValue("得分");
            c1.setCellStyle(style);

            Cell c2 = row3.createCell(5 + 2 * i);
            c2.setCellValue("结果");
            c2.setCellStyle(style);
        }

        //第三部分-3
        for (int i = 0; i < 9; i++) {
            Cell c1 = row3.createCell(10 + i * 3);
            c1.setCellValue("因子名称");
            c1.setCellStyle(style);

            Cell c2 = row3.createCell(11 + i * 3);
            c2.setCellValue("得分");
            c2.setCellStyle(style);

            Cell c3 = row3.createCell(12 + i * 3);
            c3.setCellValue("结果");
            c3.setCellStyle(style);
        }

        // 合并单元格
        for (int i = 0; i < 4; i++) {
            sheet.addMergedRegion(new CellRangeAddress(0, 2, i, i)); // 姓名、门诊号、住院号、就诊卡号
        }

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 4, 9)); // 社交能力
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 10, 36)); // 行为问题

        for (int i = 0; i < 3; i++) {
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 4+2*i, 5+2*i)); // 社交能力-123
        }

        for (int i = 0; i < 9; i++) {
            sheet.addMergedRegion(new CellRangeAddress(1, 1, 10+3*i, 12+3*i)); // 行为因子-1-9
        }

        // 写入文件
        try (FileOutputStream fileOut = new FileOutputStream("D:\\o1.xlsx")) {
            workbook.write(fileOut);
        }

        // 关闭工作簿
        workbook.close();
    }
}
